<!--
 * @Descripttion:
 * @version:
 * @Author: YouHuJun
 * @Date: 2021-09-22 14:51:37
 * @LastEditors: youhujun 2900976495@qq.com
 * @LastEditTime: 2025-05-11 04:06:41
 * @LastEditTime: 2021-09-23 18:51:53
-->
<template>
  <div>
    <el-form
      ref="addSpecValueForm"
      :model="addSpecValueForm"
      :rules="addSpecValueRules"
      label-width="100px"
      :inline="true"
      label-position="left"
      status-icon
      inline-message
      style="width:60%;margin-left:auto;margin-right: auto;padding:10px 10px;"
    >
      <el-row type="flex">
        <el-col :span="24" class="center">
          <el-form-item label="规格值" prop="spec_value">
            <el-input v-model="addSpecValueForm.spec_value" placeholder="一般为汉字" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="24" class="center">
          <el-form-item label="规格值类型" prop="value_type">
            <el-select v-model="addSpecValueForm.value_type" placeholder="请选择" disabled>
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="24" class="center">
          <el-form-item label="备注" prop="note">
            <el-input
              v-model="addSpecValueForm.note"
              type="textarea"
              :rows="1"
              :autosize="{ minRows: 2, maxRows: 6 }"
              placeholder="请输入内容"
              maxlength="30"
              show-word-limit
              class="info"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="center">
        <el-col :span="12" :offset="4">
          <el-form-item>
            <el-button type="primary" @click="submitForm('addSpecValueForm')">添加</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="12" :offset="4">
          <el-form-item>
            <el-button @click="resetForm('addSpecValueForm')">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>

import { debounce } from '@/utils'
import { addGoodsSpecValue } from '@/api/youhu-shop/V1/goods/goodsSpec'
import permission from '@/directive/permission/index.js' // 权限判断指令
export default
{
  // 组件名称
  name: 'AddSpecValueForm',
  // 组件
  components:
    {
    },
  directives: { permission },
  props:
{
  specNameId: {
    type: [String, Number],
    default: 0
  }
},

  // 数据
  data()
  {
    return {
      // 添加菜单表单
      addSpecValueForm: {
        goods_spec_name_id: 0,
        spec_value: '',
        value_type: 10,
        note: '',
        sort: 100

      },
      // 添加表单规则
      addSpecValueRules: {
        spec_value: [
          { required: true, message: '必须有属性值', trigger: 'blur' }
        ],
        value_type: [
          { pattern: /^[0-9]+$/, message: '只能是0-9数字', trigger: 'blur' }
        ],
        note: [
          { pattern: /[.]*/, message: '随便', trigger: 'blur' }
        ]
      },
      options: [
        { value: 10, label: '值' },
        { value: 20, label: '颜色' }
      ]
    }
  },
  // 计算属性
  computed:
  {

  },
  // 监听
  watch:
{
  specNameId: {
    handler(value)
    {
      // console.log('update:' + value)
      if (parseInt(value) > 0)
      {
        this.addSpecValueForm.goods_spec_name_id = value
      }
    },
    immediate: true
  }
},
  // 实例创建之前
  beforeCreate()
  {
  },
  // 创建
  created()
  {

  },
  // 挂载之前
  beforeMount()
  {

  },
  // 挂载 --常用
  mounted()
  {

  },
  // 更新之前
  beforeUpdate()
  {
  },
  // 跟新后
  updated()
  {
  },
  // 销毁之前
  beforeDestroy()
  {
  },
  // 销毁后
  destroyed()
  {
  },
  // 方法
  methods:
  {

    initSpecValueForm()
    {
      this.addSpecValueForm = {
        id: 0,
        phone: '',
        specValue_name: '',
        specValue_code: '',
        is_default: 0,
        sort: 100
      }
    },
    // 提交表单
    submitForm: debounce(function(formName)
    {
      this.$refs[formName].validate((valid) =>
      {
        if (valid)
        {
          // console.log(this.addSpecValueForm)
          this.addSpecValue(this.addSpecValueForm)
        }
        else
        {
          console.log('未通过验证!')
          return false
        }
      })
    }, 600),

    // 重置表单
    resetForm(formName)
    {
      this.initSpecValueForm()
    },

    /**
     * 添加菜单
     */
    addSpecValue(data)
    {
      const _this = this
      addGoodsSpecValue(data).then(res =>
      {
        // console.log(res)
        if (res && res.code === 0)
        {
          _this.$emit('handleCloseDialog', false)
          _this.$emit('hanldeUpdateData', true)

          _this.$message(
            {
              message: res.msg,
              type: 'success'
            })
          _this.initSpecValueForm()
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
 .center{
    text-align: center;
 }
  .info{
   width: 400px;
 }
</style>
